package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.OceanengineReportCreativeInfo;
import com.ylyq.ylxx.model.bean.OceanengineReportGroupInfo;
import com.ylyq.ylxx.model.bean.OceanengineReportInfo;
import com.ylyq.ylxx.model.bean.OceanengineReportPlanInfo;
import com.ylyq.ylxx.model.request.GetReportCreativeRequest;
import com.ylyq.ylxx.model.request.GetReportGroupRequest;
import com.ylyq.ylxx.model.request.GetReportPlanRequest;
import com.ylyq.ylxx.model.request.GetReportRequest;
import com.ylyq.ylxx.service.IOceanegineReportService;
import com.ylyq.ylxx.util.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName IOceanegineReportServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/2 11:46
 * @Version 1.0
 */
@Service
public class OceanegineReportServiceImpl implements IOceanegineReportService {
    @Override
    public OceanengineReportGroupInfo getReportGroup(GetReportGroupRequest getReportRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "report/campaign/get/";

        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getReportRequest).toString(), headers);


        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineReportGroupInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineReportGroupInfo.class);
        return oceanengineAccount;

    }

    @Override
    public OceanengineReportPlanInfo getReportPlan(GetReportPlanRequest getReportRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "report/ad/get/";


        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getReportRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineReportPlanInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineReportPlanInfo.class);
        return oceanengineAccount;
    }

    @Override
    public OceanengineReportCreativeInfo getReportCreative(GetReportCreativeRequest getReportRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "report/creative/get/";


        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getReportRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineReportCreativeInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineReportCreativeInfo.class);
        return oceanengineAccount;
    }

    @Override
    public ResponseData getReport(GetReportRequest getReportRequest) {
        // 请求地址
        String open_api_url_prefix = AccountConstant.OCEANENGINE_URL + "2/";
        String uri = "report/integrated/get/";


        // 构造请求
        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Token", FilterContextHandler.getOceaneToken());

        String result = HttpClientUtils.get(open_api_url_prefix + uri, JSONObject.toJSON(getReportRequest).toString(), headers);

        JSONObject jsonObject = JSONObject.parseObject(result);
        OceanengineReportInfo oceanengineAccount = JSONObject.toJavaObject(jsonObject, OceanengineReportInfo.class);
        return new ResponseData(ResponseData.Result.SUCCESS, oceanengineAccount);
    }
}
